#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/wait.h>
#include <signal.h>

void warning(int a)
{
    printf("网络中断\n");
}

int main(int argc, char const *argv[])
{
    int fd[2];
    pipe(fd);    
    //printf("%d\n%d\n",fd[0],fd[1]);
    pid_t pid = fork();
    if (pid == 0)
    {
        close(fd[1]);
        //dup2(fd[0],0);
        char buff[24] = "";
        while(1)
        {
            read(fd[0],buff,24);
            printf("子进程%d读到的内容是%s\n",getpid(),buff);
        }
        close(fd[0]);
    }
    else if (pid > 0)
    {
        close(fd[0]);
        //dup2(fd[1],1);
        signal(SIGPIPE,warning);
        char a[24]="";
        while (1)
        {
            scanf("%s",a);
            write(fd[1],a,24);
            sleep(2);
        }

        wait(NULL);
        
        close(fd[1]);
    }

    return 0;
}
